﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.UserControls;

    public class SysSettingList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F1072;
        protected GridView gvSysSetting;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2SysSettingList(SysSettingQueryEntity SysSettingQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(SysSettingQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(SysSettingQuery, "SYS_SETTING");
            base.BindData2GridView(this.gvSysSetting, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            SysSettingQueryEntity SysSetting = this.GetSysSettingQueryEntity();
            SysSetting.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = SysSetting.CurrentPage;
            SysSetting.CurrentPage = "0";
            this.gvSysSetting.AllowSorting = false;
            this.BindData2SysSettingList(SysSetting);
            this.gvSysSetting.ShowFooter = false;
            this.hdnActionID.Value = "";
            SysSetting.IsGetAll = false;
            SysSetting.CurrentPage = currentPageIndex;
            this.SetSysSettingEntity(SysSetting);
            base.Export2Excel("SysSetting.xls", this.gvSysSetting);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            SysSettingQueryEntity SysSettingQuery = this.GetSysSettingQueryEntity();
            SysSettingQuery.CurrentPage = "0";
            this.BindData2SysSettingList(SysSettingQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            SysSettingQueryEntity SysSettingQuery = this.GetSysSettingQueryEntity();
            SysSettingQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2SysSettingList(SysSettingQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            SysSettingQueryEntity SysSetting = this.GetSysSettingQueryEntity();
            SysSetting.SysSettingID = "";
            SysSetting.Value = "";
            SysSetting.ValueDefault = "";
            SysSetting.ParamType = "";
            SysSetting.IsModifiable = "";
            SysSetting.Effective = "";
            this.BindData2SysSettingList(SysSetting);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            SysSettingQueryEntity SysSettingQuery = this.GetSysSettingQueryEntity();
            if (int.Parse(SysSettingQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                SysSettingQuery.CurrentPage = (int.Parse(SysSettingQuery.CurrentPage) + 1).ToString();
                this.BindData2SysSettingList(SysSettingQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            SysSettingQueryEntity SysSettingQuery = this.GetSysSettingQueryEntity();
            if (int.Parse(SysSettingQuery.CurrentPage) > 0)
            {
                SysSettingQuery.CurrentPage = (int.Parse(SysSettingQuery.CurrentPage) - 1).ToString();
                this.BindData2SysSettingList(SysSettingQuery);
            }
        }

        private SysSettingQueryEntity GetSysSettingQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as SysSettingQueryEntity);
            }
            return (base.PageQueryEntitySession as SysSettingQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                SysSettingQueryEntity SysSettingQuery = this.GetSysSettingQueryEntity();
                SysSettingQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2SysSettingList(SysSettingQuery);
            }
        }

        protected void gvSysSetting_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F1072", this.gvSysSetting);
        }

        protected void gvSysSetting_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvSysSetting_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                SysSettingQueryEntity sysSetting = this.GetSysSettingQueryEntity();
                sysSetting.SortField = e.SortExpression;
                if (sysSetting.SortDirection == "ASC")
                {
                    sysSetting.SortDirection = "DESC";
                }
                else if (sysSetting.SortDirection == "DESC")
                {
                    sysSetting.SortDirection = "ASC";
                }
                this.BindData2SysSettingList(sysSetting);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            SysSettingQueryEntity SysSetting = new SysSettingQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    SysSetting.SysSettingID = this.PreviousPage.SysSettingID;
                    SysSetting.Value = this.PreviousPage.Value;
                    SysSetting.ValueDefault = this.PreviousPage.ValueDefault;
                }
                else
                {
                    SysSetting.SysSettingID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            SysSetting.IsGetAll = false;
            SysSetting.CurrentPage = "0";
            SysSetting.PageSize = this.gvSysSetting.PageSize.ToString();
            SysSetting.SortField = "SS.ROWID";
            SysSetting.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvSysSetting.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetSysSettingEntity(SysSetting);
            this.BindData2SysSettingList(SysSetting);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetSysSettingEntity(SysSettingQueryEntity SysSettingQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = SysSettingQuery;
            }
            else
            {
                base.PageQueryEntitySession = SysSettingQuery;
            }
        }

        public SysSettingQuery PreviousPage
        {
            get
            {
                return (SysSettingQuery) base.PreviousPage;
            }
        }
    }
}

